package tw.asap.plus.ui.custom;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;

/**
 * Description:
 * Created:      [15/11/19]
 * Author:       [Yang]
 **/
public class CategorySelectorView extends PopupWindow implements View.OnClickListener {

    private final View rootView;
    @InjectView(R.id.cat_shop)
    TextView catShop;
    @InjectView(R.id.cat_goods)
    TextView catGoods;
    AdapterView.OnItemClickListener listener;

    public CategorySelectorView(Context context) {
        super(context);
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new BitmapDrawable());// 这样设置才能点击屏幕外dismiss窗口
        this.setOutsideTouchable(true);
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        rootView = mLayoutInflater.inflate(R.layout.view_category_selector, null);
        ButterKnife.inject(this, rootView);
        catShop.setOnClickListener(this);
        catGoods.setOnClickListener(this);
        setContentView(rootView);
    }

    public void setItemClickListener(AdapterView.OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cat_shop:
                if (listener != null) {
                    listener.onItemClick(null, catShop, 0, R.id.cat_shop);
                    dismiss();
                }
                break;

            case R.id.cat_goods:
                if (listener != null) {
                    listener.onItemClick(null, catShop, 1, R.id.cat_goods);
                    dismiss();
                }
                break;
        }
    }
}
